package com.wsh.controller.sys;


import com.wsh.dto.sys.UserDto;
import com.wsh.utils.JsonResult;
import com.wsh.vo.sys.RePwdVo;
import com.wsh.vo.sys.UserVo;
import com.wsh.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author 王思涵
 * @Date 2019/7/20
 * @Version 1.0
 * @Desciption
 **/
@RestController
@RequestMapping("/sys/user")
@Api(tags = "系统：用户管理")
public class UserController {


    @Value("${rsa.private_key}")
    private String privateKey;

    /**
     * 用户业务
     */
    @Autowired
    private UserService userService;

    /**
     * 用户列表
     */
    @GetMapping("/list")
    @PreAuthorize("@el.check('user:list')")
    @ApiOperation("用户列表")
    public Object list(UserDto userDto) {
        return JsonResult.success(userService.findListForPage(userDto));
    }

    /**
     * 添加用戶
     */
    @PostMapping("/saveUser")
    @PreAuthorize("@el.check('user:save')")
    @ApiOperation("添加用户")
    public Object addUser(@RequestBody UserVo userVo) {
        return JsonResult.success(userService.saveUser(userVo));
    }

    /**
     * 查询用户
     */
    @GetMapping("/getById")
    @ApiOperation("查询用户")
    public Object getById(@RequestParam String formId) {
        return JsonResult.success(userService.findById(formId));
    }

    /**
     * 修改密码
     */
    @PreAuthorize("@el.check('user:rePwd')")
    @PostMapping("/rePwd")
    @ApiOperation("密码修改")
    public Object rePwd(RePwdVo rePwdVo) {
        try {
            rePwdVo.setPrivateKey(privateKey);
            return JsonResult.success(userService.rePwd(rePwdVo) ? "密码修改成功，页面刷新重新登陆" : "密码修改出错，重新登陆");
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation("修改头像")
    @PostMapping(value = "/test")
    public Object updateAvatar(@RequestParam MultipartFile file){
        return file.getOriginalFilename();
    }

}
